#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <linux/ioctl.h>
#include <unistd.h>

#define LEDOFF 0
#define LEDON 1
#define CLOSE_CMD 		(_IO(0XEF, 0x1)) 	/* 关闭定时器 */
#define OPEN_CMD 		(_IO(0XEF, 0x2)) 	/* 打开定时器 */
#define SETPERIOD_CMD 	(_IO(0XEF, 0x3)) 	/* 设置定时器周期命令 */


/* ./app /dev/* 1 5000 */

int main(int argc, char *argv[])
{
    int fd, retvalue;
    char *filename;
    int checksum;
    int humidity,temperature;
    unsigned char data[5];
    filename = argv[1];

    fd = open(filename, O_RDWR);
    if (fd < 0)
    {
        printf("file %s open failed!\r\n", argv[1]);
        return -1;
    }
    while(1)
    {
        retvalue = read(fd, data, sizeof(data));
        // 验证校验和
        checksum = data[0] + data[1] + data[2] + data[3];
        if (checksum != data[4])
        {
            printf("checksum is error!\r\n");
            return -1; // 校验和错误
        }
        // 提取湿度和温度值
        humidity = (int)data[0];
        temperature = (int)data[2];
        printf("humidity: %d  temperature:%d \r\n",humidity,temperature);
        sleep(1);
    }
    retvalue = close(fd); /* 关闭文件 */
    if (retvalue < 0)
    {
        printf("file %s close failed!\r\n", argv[1]);
        return -1;
    }
    return 0;
}